﻿using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Uow;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.PostgreSql;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Himp.ShareData;

namespace Himp.Platform.EntityFrameworkCore;

[DependsOn(
    typeof(PlatformDomainModule),
    typeof(AbpIdentityEntityFrameworkCoreModule),
    typeof(AbpOpenIddictEntityFrameworkCoreModule),
    typeof(AbpPermissionManagementEntityFrameworkCoreModule),
    typeof(AbpSettingManagementEntityFrameworkCoreModule),
    typeof(AbpEntityFrameworkCorePostgreSqlModule),
    typeof(AbpBackgroundJobsEntityFrameworkCoreModule),
    typeof(AbpAuditLoggingEntityFrameworkCoreModule),
    typeof(AbpTenantManagementEntityFrameworkCoreModule),
    typeof(AbpFeatureManagementEntityFrameworkCoreModule),
    typeof(BlobStoringDatabaseEntityFrameworkCoreModule)
    )]
public class PlatformEntityFrameworkCoreModule : AbpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
        PlatformEfCoreEntityExtensionMappings.Configure();
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAbpDbContext<PlatformDbContext>(options =>
        {
            /* Remove "includeAllEntities: true" to create
                * default repositories only for aggregate roots */
            options.AddDefaultRepositories(includeAllEntities: true);


            options.Entity<CatgeDict>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.Children).ThenInclude(p => p.Children).ThenInclude(p => p.Children); };
            });

            options.Entity<CodgDict>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.CodgDictRules).Include(p => p.CodgHistorys); };
            });

            options.Entity<Menu>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => q.Include(p => p.Children).ThenInclude(p => p.Children);
            });

            options.Entity<StdPhrDict>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.StdDomainDict); };
            });

            options.Entity<DataModelPropertyDict>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.StdPhrDict).ThenInclude(p => p.StdDomainDict); };
            });

            options.Entity<DataModelExtPropertyDict>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.StdPhrDict).ThenInclude(p => p.StdDomainDict); };
            });

            options.Entity<DataTableDict>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.DataTableColumns); };
            });

            options.Entity<DataTableRestrRule>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.DataTableColumn); };
            });

            #region 患者

            options.Entity<PatnMasterIdx>(opt =>
            {
                opt.DefaultWithDetailsFunc = q => { return q.Include(p => p.PatnCardInfoes).Include(p => p.PatnBaseInfo); };
            });

            #endregion
        });

        Configure<AbpDbContextOptions>(options =>
        {
                /* The main point to change your DBMS.
                 * See also PlatformMigrationsDbContextFactory for EF Core tooling. */
            options.UseNpgsql();
        });

    }
}
